//
//  InternalMemberViewController.swift
//  willdo
//
//  Created by zz on 3/31/18.
//  Copyright © 2018 will do. All rights reserved.
//

import UIKit

class InternalMemberViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView:UITableView!
    var presentController:AddTaskViewController?
    var GroupIsOn = false
    var Group = CONTACT_GROUP
    var GroupOn = [Bool]()
//    var detail = CONTACT_DETAIL
//    var task = CONTACT_TASK
    var modle = CONTACT_INTERNAL_MODLE
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = BACKGROUND_GRAY
        tableView = UITableView(frame: CGRect.zero, style: .plain)
        tableView.backgroundColor = BACKGROUND_GRAY
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        let ui = UIView(frame: CGRect(x: 0, y: 0, width: 0, height: 10))
        ui.backgroundColor = UIColor.clear
        tableView.tableHeaderView = ui
        tableView.dataSource = self
        tableView.delegate = self
        for _ in Group{
            GroupOn.append(GroupIsOn)
        }
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "contact")
        if cell == nil{
            cell = UITableViewCell(style: .subtitle, reuseIdentifier: "contact")
        }
        cell?.textLabel?.text = modle[indexPath.section][indexPath.row].name
        cell?.textLabel?.textColor = UIColor(hexString: "#2f3f59")
        cell?.detailTextLabel?.text = modle[indexPath.section][indexPath.row].task
        cell?.detailTextLabel?.textColor = UIColor(hexString: "#9d9fa8")
        cell?.imageView?.image = UIImage(named: "temp")
        return cell!
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headView = UIView()
        headView.backgroundColor = UIColor.white
        let titleLable = UILabel()
        titleLable.text = Group[section]
        titleLable.font = UIFont(name: "PingFang SC", size: 17)
        titleLable.textColor = UIColor(hexString: "#000000")
        headView.addSubview(titleLable)
        titleLable.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(20)
        }
        
        let arrowImg = UIImageView(image: UIImage(named: "more_button"))
        headView.addSubview(arrowImg)
        arrowImg.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
            make.width.equalTo(9)
            make.height.equalTo(16)
        }
        if(GroupOn[section]){
            arrowImg.transform = CGAffineTransform(rotationAngle: .pi / 2)
        }
        let clickBtn = UIButton()
        clickBtn.backgroundColor = UIColor.clear
        headView.addSubview(clickBtn)
        clickBtn.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        clickBtn.tag = section
        clickBtn.addTarget(self, action: #selector(switchState(sender:)), for: .touchUpInside)
        return headView
    }
    
    @objc func switchState(sender : UIButton){
        GroupOn[sender.tag] = !GroupOn[sender.tag]
        
        tableView.reloadData()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 47
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 35
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView(frame: CGRect.zero)
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.00001
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return GroupOn[section] ? modle[section].count : 0
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return Group.count
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if presentController != nil {
            presentController?.selectMember = modle[indexPath.section][indexPath.row]
            navigationController?.popViewController(animated: true)
            return
        }

        let internalVC = UIViewController()
        internalVC.view = InternalMemberView(frame: view.frame, model: modle[indexPath.section][indexPath.row])
        internalVC.edgesForExtendedLayout = .init(rawValue: 0)
        internalVC.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(internalVC, animated: true)
        tableView.cellForRow(at: indexPath)?.isSelected = false
    }
    
}
